package com.jk1.bianligou.blg.controller;

import cn.hutool.core.bean.BeanUtil;
import com.jk1.bianligou.blg.aop.MerchantCheck;
import com.jk1.bianligou.blg.common.BaseResponse;
import com.jk1.bianligou.blg.domain.dto.item.ItemBatchDisableDTO;
import com.jk1.bianligou.blg.domain.dto.item.ItemCreateDTO;
import com.jk1.bianligou.blg.domain.dto.item.ItemUpdateDTO;
import com.jk1.bianligou.blg.domain.po.Item;
import com.jk1.bianligou.blg.service.ItemService;
import com.jk1.bianligou.blg.utils.ResultUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author WHH
 */
@RestController
@RequestMapping("/item")
public class ItemController {

    @Resource
    private ItemService itemService;

    @GetMapping("/show")
    @ApiOperation("按分类分组展示所有商品")
    public BaseResponse<Map<Long, List<Item>>> listAllGroupByCategory() {
        List<Item> items = itemService.lambdaQuery()
                .eq(Item::getStatus, 1)
                .list();
        // 按 categoryId 分组
        Map<Long, List<Item>> itemsByCategory = items.stream()
                .collect(Collectors.groupingBy(Item::getCategoryId));
        return ResultUtils.success(itemsByCategory);
    }

    @GetMapping("/show/admin")
    @ApiOperation("按分类分组展示所有商品(商家)")
    @MerchantCheck
    public BaseResponse<Map<Long, List<Item>>> listAllGroupByCategoryAdmin() {
        List<Item> items = itemService.lambdaQuery()
                .list();
        // 按 categoryId 分组
        Map<Long, List<Item>> itemsByCategory = items.stream()
                .collect(Collectors.groupingBy(Item::getCategoryId));
        return ResultUtils.success(itemsByCategory);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据商品id获取商品")
    @MerchantCheck
    public BaseResponse<Item> getById(@PathVariable Long id) {
        return ResultUtils.success(itemService.getById(id));
    }

    @ApiOperation("添加商品")
    @MerchantCheck
    @PostMapping
    public BaseResponse<Boolean> create(@RequestBody ItemCreateDTO product) {
        Item item = new Item();
        BeanUtil.copyProperties(product, item);
        return ResultUtils.success(itemService.save(item));
    }

    @ApiOperation("更新商品")
    @MerchantCheck
    @PutMapping("/{id}")
    public BaseResponse<Boolean> update(
            @PathVariable Long id,
            @RequestBody ItemUpdateDTO product) {
        Item item = new Item();
        item.setId(id);
        BeanUtil.copyProperties(product, item);
        return ResultUtils.success(itemService.updateById(item));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除商品")
    @MerchantCheck
    public BaseResponse<Boolean> delete(@PathVariable Long id) {
        return ResultUtils.success(itemService.removeById(id));
    }

    @PostMapping("/batch-disable")
    @MerchantCheck
    public BaseResponse<Boolean> batchDisable(@RequestBody ItemBatchDisableDTO request) {
        return ResultUtils.success(itemService.update()
                .set("status", 0)
                .in("id", request.getIds())
                .update());
    }


}
